using Mathmall.Administration.common;
using Mathmall.Administration.Service;
using Mathmall.Administration.Vo;
using Microsoft.AspNetCore.Mvc;

namespace Mathmall.Administration.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AttrController : ControllerBase
    {
        private readonly IAttrService _attrService;
        private readonly IAttrGroupService _attrGroupService;

        public AttrController(IAttrService attrService, IAttrGroupService attrGroupService)
        {
            _attrService = attrService;
            _attrGroupService = attrGroupService;
        }

        /// <summary>
        /// 保存属性信息
        /// </summary>
        /// <param name="attrVo"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult<int>> AddAttr(AttrVo attrVo)
        {
            await _attrService.AddAttr(attrVo);
            return CreatedAtAction("AddAttr", null);
        }

        /// <summary>
        ///  根据分类id查询属性信息
        /// </summary>
        /// <param name="catalogId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ActionResult<List<AttrVo>?>> QueryAttrByCatalogId(int catalogId)
        {
            List<AttrVo>? attrVoList = await _attrService.GetAttrByCatalogId(catalogId);
            if (attrVoList == null)
            {
                return NotFound();
            }

            return Ok(attrVoList);
        }

        /// <summary>
        /// 分页查询属性
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="catalogID"></param>
        /// <param name="condition"></param>
        /// <returns></returns>
        [HttpGet("[action]")]
        public async Task<PaginatedList<AttrVo>> PageNationQuery(int pageIndex, int pageSize,
            int catalogID, string condition = "")
        {
            return await _attrService.PageNationQuery(pageIndex, pageSize, catalogID, condition);
        }

        /// <summary>
        /// 修改属性值
        /// </summary>
        /// <param name="attrVo"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<ActionResult> UpdateAttr(AttrVo attrVo)
        {
            await _attrService.UpdateAttr(attrVo);
            return Ok();
        }

        /// <summary>
        /// 删除属性
        /// </summary>
        /// <param name="attrIds"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<ActionResult> DeleteAttr(List<int> attrIds)
        {
            await _attrService.DeleteAttr(attrIds);
            return Ok();
        }

        /// <summary>
        /// 根据分类id查询属性分组
        /// </summary>
        /// <param name="catalogID"></param>
        /// <returns></returns>
        [HttpGet("{catalogID}")]
        public async Task<ActionResult<List<AttrGroupVo>>> QueryAttrGroupsByCatalogId(int catalogID)
        {
            return await _attrGroupService.GetAttrGroupsByCatalogID(catalogID);
        }
    }
}